// pages/my/calcHistory/calcHistory.js
const app = getApp();
var util = require('../../util/util.js');
var date = require('../../util/date.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],//历史记录
    offset: 1,
    limit: 10,
    hasMoreData: true,//是否有更多数据
    total: 0,//数据总数
    isList: true,
    isFast: false,
  },  
  //获取历史计算 需要登录
  getCalcHistory: function (offset, limit) {
    let openId = (wx.getStorageSync('openId'));
    const that = this;
    // 拼接请求url
    const url = app.globalData.api + "/wechat/calculation/list";
    // 请求数据
    wx.request({
      url: url,
      method: "POST",
      data: { "offset": offset, "limit": limit, "userOpenid": openId },
      success: function (res) {
        if (res.data && res.data.rows) {
          //处理数据
          res.data.rows.forEach((item, index) => {
            //处理到期日期
            if (!item.expirationDate){
              item.expiringDate = ""
            }else{
              item.expiringDate = date.formatDate(item.expirationDate, 'yyyy-MM-dd');
            }
            //处理计算日期
            if (!item.createTime) {
              item.calcTime = ""
            } else {
              item.calcTime = date.formatDate(item.createTime, 'yyyy-MM-dd hh:mm');
            }
          })
        }
        var listTem = that.data.list;
        if (res.data && res.data.rows && res.data.total) {
          // 赋值
          if (that.data.offset == 1) {
            listTem = []
          }
          var list = res.data.rows;
          // console.info(list);
          //是否有多页数据
          if (res.data.total < that.data.limit) {
            //只有一页
            // console.info("只有一页");
            that.setData({
              isList: true,
              list: listTem.concat(list),
              total: res.data.total,
              hasMoreData: false
            })
          }
          //当前页小于总页数 可继续加载
          if (that.data.offset < Math.ceil(res.data.total / that.data.limit)) {
            // console.info("共有 " + Math.ceil(res.data.total / that.data.limit) + " 页数据,现在加载第 " + that.data.offset + " 页");
            that.setData({
              isList: true,
              list: listTem.concat(list),
              total: res.data.total,
              hasMoreData: true,
              offset: that.data.offset + 1,
            })
          } else {
            //最后一页
            // console.info("最后一页了");
            that.setData({
              hasMoreData: false
            })
          }
        } else {
          //空的
          that.setData({
            isList: false,
            list: [],
            total: 0,
          })
        }
      }
    })
  },
  //跳转计算详情
  toCalcDetail(e) {
    var oData = e.currentTarget.dataset;
    wx.navigateTo({
      url: '../../index/calcDetail/calcDetail?id=' + oData.id
    })
  },
  //删除当前计算结果
  deleteIt(e) {
    var that = this;
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '注意',
      content: '此操作将永久删除此数据，是否继续？',
      success: function (res) {
        if (res.confirm) {
          // 拼接请求url
          const url = app.globalData.api + "/wechat/calculation/remove/"+id;
          // 请求数据
          wx.request({
            url: url,
            method: "get",
            success: function (res) {
              if (res.data.code == 0) {
                let long = that.data.list.length;
                if (long == 1) {
                  that.setData({
                    isList: false
                  })
                }else{
                  that.setData({
                    isList: true
                  })
                }
                that.data.list.splice(index,1);
                wx.showToast({
                  title: "删除成功！",
                  icon: "none"
                })
              }else{
                wx.showToast({
                  title: res.data.msg,
                  icon: "none"
                })
              }
            },fail(err){
              wx.showToast({
                title: "网络错误，请稍后重试~",
                icon: "none"
              })
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //初始化加载
    this.getCalcHistory(1, 10)
  },
  /**
  * 页面相关事件处理函数--监听用户下拉动作 (刷新页面)
  */
  onPullDownRefresh: function () {
    this.setData({
      offset: 1
    })
    this.getCalcHistory(1, 10);
    wx.showLoading({
      title: '正在刷新数据',
    })
    setTimeout(function () {
      wx.hideLoading();
      wx.stopPullDownRefresh();
    }, 2000)
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const that = this;
    if (that.data.hasMoreData) {
      //存在多页数据
      that.getCalcHistory(that.data.offset, that.data.limit);
      wx.showLoading({
        title: '正在加载',
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 1000)
    } else {
      wx.showToast({
        title: '没有更多数据了~',
        icon: 'none',
      })
    }
  },
  //快速导航的点击
  tapIn(e) {
    let index = e.currentTarget.dataset.index;
    if (index=="1") {
      //收起
      this.setData({
        isFast: false
      })
    } else {
      //展开
      this.setData({
        isFast: true
      })
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})